package com.youwu.weather.job;

import com.youwu.weather.service.CityClient;
import com.youwu.weather.service.WeatherDataCollectionService;
import com.youwu.weather.vo.City;
import com.youwu.weather.vo.WeatherType;
import org.apache.commons.collections4.CollectionUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

public class WeatherDataSyncJob extends QuartzJobBean {

    private static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);

    @Autowired
    private WeatherDataCollectionService weatherDataCollectionService;
    @Autowired
    private CityClient cityClient;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("quartz has done");

        // 由城市数据微服务来提供城市数据
        List<City> cityList = cityClient.listCity();
        // 遍历城市
        if (CollectionUtils.isNotEmpty(cityList)) {
            logger.info("城市数据获取成功！");
            for (int i=0; i < cityList.size(); i++) {
//                weatherDataCollectionService.syncDataByCity(WeatherType.NOW, cityList.get(i).getCityId());
                System.out.println(cityList.get(i).getCityCn());
            }

        } else {
            logger.error("城市数据获取失败！");
        }
        // 由于api接口调用限制，这边只获取一条数据用来测试
        weatherDataCollectionService.syncDataByCity(WeatherType.NOW, "思明区,厦门");
    }
}
